package com.laoliu.example.generate.constant;

import com.laoliu.example.generate.bean.vo.GenerateParamVO;
import com.laoliu.example.generate.utils.StringUtils;
import org.springframework.util.PropertyPlaceholderHelper;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * 配置信息变量
 * Created by liuchunchun on 2019/02/14.
 */
public class CodeGeneratorConst {
	// JDBC 相关配置信息
	protected static String JDBC_URL;
	protected static String JDBC_USERNAME;
	protected static String JDBC_PASSWORD;
	protected static String JDBC_DRIVER_CLASS_NAME;

	// 项目基础包
	protected static String BASE_PACKAGE;
	// 模板注释中 @date
	protected static String DATE;

	// ************************************************非传递参数****************************************
	protected static String SRC_MAIN_JAVA="src/main/java";
	protected static String AUTHOR;
	//项目模块路径
	protected static String PROJ_PATH_JAVA;
	//项目模块路径
	protected static String PROJ_PATH_RESOURCE;
    //接口模块路径
	protected static String API_PATH_JAVA;
	//控制类路径
	protected static String CONTROLLER_PATH;
    //控制类包名
	protected static String CONTROLLER_PACKAGE;
	//model路径名
	protected static String MODEL_PATH;
	//model包名
	protected static String MODEL_PACKAGE;
	//service接口路径名
	protected static String SERVICE_API_PATH;
	//service接口包名
	protected static String SERVICE_API_PACKAGE;
	//service实现类路径名
	protected static String SERVICE_IMPL_PATH;
	//service实现类包名
	protected static String SERVICE_IMPL_PACKAGE;
	//mapper路径名
	protected static String MAPPER_PATH;
	//mapper包名
	protected static String MAPPER_PACKAGE;

	static {
		init();
	}

	private static Properties prop;
	/**
	 * 初始化配置信息
	 */
	private static void init() {
		prop = loadProperties();
		JDBC_URL = getProperty("jdbc.url");
		JDBC_USERNAME = getProperty("jdbc.username");
		JDBC_PASSWORD = getProperty("jdbc.password");
		JDBC_DRIVER_CLASS_NAME = getProperty("jdbc.driver.class.name");
		AUTHOR = getProperty("generator.author");
	}

	public static void initPackage(GenerateParamVO paramVO) {
		BASE_PACKAGE = paramVO.getBasePackage();
		PROJ_PATH_JAVA = paramVO.getBasePath()+"/"+paramVO.getProjName()+"/src/main/java/";
		PROJ_PATH_RESOURCE = paramVO.getBasePath()+"/"+paramVO.getProjName()+"/src/main/resources/";
		API_PATH_JAVA = paramVO.getBasePath()+"/"+paramVO.getApiName()+"/src/main/java/";
		DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());

		CONTROLLER_PATH = PROJ_PATH_JAVA+BASE_PACKAGE.replace(".","/")+"/web/";
		CONTROLLER_PACKAGE = BASE_PACKAGE+".web";
		SERVICE_IMPL_PATH = PROJ_PATH_JAVA+BASE_PACKAGE.replace(".","/")+"/service/impl/";
		SERVICE_IMPL_PACKAGE = BASE_PACKAGE+".service.impl";
		SERVICE_API_PATH = PROJ_PATH_JAVA+BASE_PACKAGE.replace(".","/")+"/service/api/";
		SERVICE_API_PACKAGE = BASE_PACKAGE+".service.api";
		MODEL_PATH = API_PATH_JAVA+BASE_PACKAGE.replace(".","/")+"/bean/model/";
		MODEL_PACKAGE = BASE_PACKAGE+".bean.model";
		MAPPER_PATH = PROJ_PATH_JAVA+BASE_PACKAGE.replace(".","/")+"/mapper/";
		MAPPER_PACKAGE = BASE_PACKAGE+".mapper";
	}

	/**
	 * 加载配置文件
	 * @return
	 */
	private static Properties loadProperties() {
		Properties prop = null;
		try {
			prop = new Properties();
			InputStream in = CodeGeneratorConst.class.getClassLoader().getResourceAsStream("application.properties");
			prop.load(in);
		} catch (Exception e) {
			throw new RuntimeException("加载配置文件异常!", e);
		}
		return prop;
	}

	private static String getProperty(String name) {
		PropertyPlaceholderHelper placeholder = new PropertyPlaceholderHelper( "${", "}" );
		String value = prop.get(name) == null? null: prop.get(name).toString();
		if(StringUtils.isNullOrEmpty(value)){
			return value;
		}
		return placeholder.replacePlaceholders(value, prop);
	}
}
